<button class="btn btn-primary" type="button" onclick="addProduct()">新增</button>

<table class="table" id="userListDiv">
    <thead>
    <tr>
        <th>序号</th>
        <th>产品编号</th>
        <th>产品类别序列号</th>
        <th>产品类别</th>
        <th>名称</th>
        <th>数量单位</th>
        <th>销售价</th>
        <th>添加日期</th>
        <th>产品描述</th>
        <th>库存数</th>
        <th>采购在途数</th>
        <th>销售代发数</th>
        <th colspan="2">操作</th>
    </tr>
    </thead>
    <tbody id="tableBody"></tbody>
</table>
<div>
    <ul id="page">分页</ul>
</div>

<script id="productListTemplate" type="text/html">
    {{each data a b}}
    {{each a.productList value index}}
    <tr>
        <td>{{b+1}}</td>
        <td>{{value.productCode}}</td>
        <td>{{value.categoryId}}</td>
        <td>{{a.name}}</td>
        <td>{{value.name}}</td>
        <td>{{value.unitName}}</td>
        <td>{{value.price}}</td>
        <td>{{value.createDate}}</td>
        <td>{{value.remark}}</td>
        <td>{{value.num}}</td>
        <td>{{value.poNum}}</td>
        <td>{{value.soNum}}</td>
        <td><input value="删除" type="button"  onclick="return del('{{value.productCode}}')"></td>
        <td><input value="修改" type="button"  onclick="update('{{value.productCode}}')"></td>
    </tr>
    {{/each}}
    {{/each}}
</script>

<script>
    $(function () {
        showPage({
            currentPage: 1,
            pageSize: 2,
            url: '/getAllProduct',
            templateId: 'productListTemplate',
            contentId: 'tableBody',
            params: {
                id: 1
            }
        })
    })
    function del(obj) {
        if(confirm("确认删除？")){
            Ajax.post(
                "/deleteProduct",
                {productCode:obj},
                function (result) {
                    show('category/product.html')
                    alert(result.msg)
                }
            )
        }else{
            show('category/product.html')
            return false;
        }

    }
    function update(obj) {
        Ajax.getHtml('category/productUpdate.html', function (html) {
            layer.open({
                type: 1,
                title: '修改产品',
                area: ['600px'],
                content: html,
                btn: ['确认', '取消'],
                yes: function () {
                    updateproduct()
                },
                btn2: function () {
                    console.log('取消修改产品')
                }
            })
        })
        Ajax.post(
            "selectByProductCode",
            {productCode:obj},
            function (result) {
                data=result.data[0];
                $("#productCode").val(data.productCode);
                $("#productName").val(data.name);
                $("#unitName").val(data.unitName);
                $("#price").val(data.price);
                $("#createDate").val(data.createDate);
                $("#remark").val(data.remark);
                $("#num").val(data.num);
                $("#poNum").val(data.poNum);
                $("#soNum").val(data.soNum);
            }
        )
        Ajax.post(
            "selectCategoryIdByProductCode",
            {productCode:obj},
            function (result) {
                data=result.data[0];
                $("input[type=radio][unchecked][value=data]").attr("checked",true)
            }
        )
    }
</script>
